package fr.pud.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.PUDStationDatabase;

/**
 * Apr 9, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         Tools4PUDStation.java
 */
public class Tools4PUDStation {
    private static File DATABASE_PUD_STATIONS_FILE = new File(
                                                           LanguagesConstants.LANGUAGE
                                                                   .getString(LanguagesConstants.FOLDER_NAME_PUD_DATA)
                                                                   + "/"
                                                                   + ".PUDStationsDatabaseFile.serial");

    /**
     * Load the database.
     * @return a HashSet of all the addresses which were saved.
     */
    @SuppressWarnings("unchecked")
    public static HashSet<PUDStation> loadPUDSationsData() {
        HashSet<PUDStation> pudStationsList = new HashSet<PUDStation>();
        if (DATABASE_PUD_STATIONS_FILE.exists()) {
            try {
                FileInputStream fis = new FileInputStream(
                        DATABASE_PUD_STATIONS_FILE);
                ObjectInputStream ois = new ObjectInputStream(fis);
                try {
                    pudStationsList = (HashSet<PUDStation>) ois.readObject();
                }
                finally {
                    try {
                        ois.close();
                    }
                    finally {
                        fis.close();
                    }
                }
            }
            catch (IOException ioe) {
                System.out.println(ioe);
                Tools.writeToLogFile("Pb lecture", true, ioe.getStackTrace());
            }
            catch (ClassNotFoundException cnfe) {
                System.out.println(cnfe);
                Tools.writeToLogFile("Pb lecture", true, cnfe.getStackTrace());
            }
        }
        return pudStationsList;
    }

    /**
     * Write in the log the file all the address of the user
     * @param addressesList
     *            , The list of address to write inside the file
     */
    public static void serializePUDStationsDatabase() {
        try {
            DATABASE_PUD_STATIONS_FILE.delete();
            DATABASE_PUD_STATIONS_FILE.createNewFile();
            Tools.createHiddenFile(DATABASE_PUD_STATIONS_FILE);
            FileOutputStream fileOutput = new FileOutputStream(
                    DATABASE_PUD_STATIONS_FILE);
            ObjectOutputStream oos = new ObjectOutputStream(fileOutput);
            oos.writeObject(PUDStationDatabase.getInstance().getStations());
            oos.flush();
            oos.close();
        }
        catch (IOException e) {
            Tools.writeToLogFile("File could not be created", false,
                    e.getStackTrace());
        }
    }
}
